package com.jzr.pda.db1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.pda.db1.mapper.MyUserMapper;
import com.jzr.pda.db1.model.TUser;
import com.jzr.pda.db1.service.TUserService;
import com.jzr.pda.db1.vo.DeptCodeVo;
import com.jzr.pda.db1.vo.MyUserDetailsVo;
import com.jzr.pda.db1.vo.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("UserService")
public class TUserServiceImpl extends ServiceImpl<MyUserMapper,TUser> implements TUserService {
    @Override
    public  List<String> getRoleCodesByUserCode(String userCode){
        return baseMapper.getRoleCodesByUserCode(userCode);
    }

    @Override
    //@Cacheable(cacheNames = {"user.getUserDetailByUserName"},key = "#userName", unless = "#result == null")
    public MyUserDetailsVo getUserDetailByUserCode(String userCode){
        TUser tUser = this.findByUserCode(userCode);

        if(tUser == null){
            //throw exception inform front end not this user
            throw new UsernameNotFoundException("用户 + " + userCode + "不存在.");
        }

        List<String> roleList = this.getRoleCodesByUserCode(userCode);
        List<GrantedAuthority> authorities = roleList.stream()
                .map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());

        //return new org.springframework.security.core.userdetails.User(userCode,tUser.getPassword(),authorities);
        return new MyUserDetailsVo(tUser.getId(),userCode,tUser.getPassword(),authorities);
    }

    @Override
    public TUser findByUserName(String userName){
        QueryWrapper<TUser> qw = new QueryWrapper<>();
        qw.eq(TUser.UserName_,userName);
        return baseMapper.selectOne(qw);
    }

    @Override
    public TUser findByUserCode(String userCode){
        QueryWrapper<TUser> qw = new QueryWrapper<>();
        qw.eq(TUser.UserCode_,userCode);
        return baseMapper.selectOne(qw);
    }


    @Override
    public Integer getIdByUserCode(String userCode) {
        return baseMapper.getIdByUserCode(userCode);
    }

    @Override
    public String getPasswordByUserCode(String userCode){
        return baseMapper.getPasswordByUserCode(userCode);
    }

    @Override
    public List<DeptCodeVo> getDeptList(String userCode){
        return baseMapper.getDeptList(userCode);
    }

    @Override
    public UserInfoVo getUserInfoVo(String userCode) {
        QueryWrapper<TUser> qw = new QueryWrapper<>();
        qw.eq(TUser.UserCode_,userCode);
        TUser user = baseMapper.selectOne(qw);
        if(user == null){
            return null;
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user,userInfoVo);

        return userInfoVo;
    }
}
